package top.lichuankai.wiki.service;

import org.springframework.stereotype.Service;
import top.lichuankai.wiki.domain.Ebook;
import top.lichuankai.wiki.domain.EbookExample;
import top.lichuankai.wiki.mapper.EbookMapper;
import top.lichuankai.wiki.req.EbookReq;
import top.lichuankai.wiki.resp.CommonResp;
import top.lichuankai.wiki.resp.EbookResp;
import top.lichuankai.wiki.util.CopyUtil;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookService {
    @Resource
    EbookMapper ebookMapper;
    public CommonResp list(EbookReq ebookReq){
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        criteria.andNameLike("%"+ebookReq.getName()+"%");
        CommonResp<List<EbookResp>> commonResp = new CommonResp<>();
        List<Ebook> ebooks = ebookMapper.selectByExample(ebookExample);
//        List<EbookResp> ebookResplist=new ArrayList<>();
//        for (Ebook ebook : ebooks) {
////            EbookResp ebookResp = new EbookResp();
////            BeanUtils.copyProperties(ebook, ebookResp);
//            //单体复制
//            EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
//            ebookResplist.add(ebookResp);
//        }

        List<EbookResp> ebookRespList = CopyUtil.copyList(ebooks, EbookResp.class);

        commonResp.setContent(ebookRespList);
        return commonResp;
    }
}
